package com.woniu.apiImpl;


import com.woniu.HisHospitalizationApi.HisConsumptionApi;
import com.woniu.HisHospitalizationApi.HisHospitalRecordsApi;
import com.woniu.HisHospitalizationApi.HisOrderFormApi;
import com.woniu.aggregation.api.ResidentDoctorQueryApi;
import com.woniu.aggregation.dto.InpatientDto;
import com.woniu.aggregation.param.*;
import com.woniu.api.HisCheckAPI;
import com.woniu.api.HisPatientQueryApi;
import com.woniu.api.HisPrescriptionQueryApi;
import com.woniu.api.PayApi;
import com.woniu.dto.*;
import com.woniu.exception.DIYException;
import com.woniu.param.*;
import com.woniu.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("inpatient-1")
public class ResidentDoctorQueryApiImpl implements ResidentDoctorQueryApi {
    //住院医嘱接口
    @Resource
    private HisOrderFormApi hisOrderFormApi;
    //住院消费接口
    @Resource
    private HisConsumptionApi hisConsumptionApi;
    //患者原子服务
    @Resource
    private HisPatientQueryApi hisPatientQueryApi;
    //患者用药表
    @Resource
    private HisPrescriptionQueryApi hisPrescriptionQueryApi;
    //患者住院记录表
    @Resource
    private HisHospitalRecordsApi hisHospitalRecordsApi;
    //流水表
    @Resource
    private PayApi payApi;

    //患者检查服务
    @Resource
    private HisCheckAPI hisCheckAPI;

    @Override
    public Result queryPatientInformation(PatientMessageParam patientMessageParam) {
        HisHospitalRecordsParam hisHospitalRecordsParam = new HisHospitalRecordsParam();
        BeanUtils.copyProperties(patientMessageParam,hisHospitalRecordsParam);
        Result<PageDTO<HisHospitalRecordsDto>> hisHospitalRecordsBy = hisHospitalRecordsApi.getHisHospitalRecordsBy(hisHospitalRecordsParam);
        return hisHospitalRecordsBy;
    }

//    @Override
//    public Result<InpatientDto> generateMedicalRecords(AddInpatientParam addInpatientParam) {
//        return null;
//    }
//
//    @Override
//    public Result<InpatientDto> modifyMedicalRecords(ModifyInpatientParam modifyInpatientParam) {
//        return null;
//    }
//
//    @Override
//    public Result<InpatientDto> modifyMedicalRecordStatus(ModifyStatusInpatientParam modifyStatusInpatientParam) {
//        return null;
//    }
//
//    @Override
//    public Result<Boolean> deleteMedicalRecord(ArrayList<Integer> ids) {
//        return null;
//    }
//
//    @Override
//    public Result<PageDTO<InpatientDto>> queryMedicalRecords(ListInpatientParam listInpatientParam) {
//        return null;
//    }

    @Override
    public Result newlyAddedDoctorsOrder(AddDoctorsOrderParam addDoctorsOrderParam) {
        Double totalPrice=0.0;
       //添加住院的医嘱表
        HisOrderFormAddParam hisOrderFormAddParam = new HisOrderFormAddParam();
        BeanUtils.copyProperties(addDoctorsOrderParam,hisOrderFormAddParam);
        log.info("医嘱表请求类:{}",hisOrderFormAddParam);
        Result<HisOrderFormDto> hisOrderFormDtoResult = hisOrderFormApi.addHisOrderForm(hisOrderFormAddParam);
        //药品清单
        List<MedicationListParam> list = addDoctorsOrderParam.getListParams();
        //添加住院的消费服务
        for (MedicationListParam medicationListParam : list) {
            totalPrice+=medicationListParam.getPrescriptionPrice().doubleValue()*medicationListParam.getPrescriptionNum();
        }

//        HisConsumptionAddParam hisConsumptionAddParam = new HisConsumptionAddParam();
//        BeanUtils.copyProperties(addDoctorsOrderParam,hisConsumptionAddParam);
//        hisConsumptionAddParam.setConsumptionPrice(new BigDecimal(totalPrice));
//        hisConsumptionAddParam.setConsumptionStatus(0);
//        hisConsumptionAddParam.setConsumptionDesc(new Date()+"用药");
//        log.info("住院消费表请求类:{}",hisConsumptionAddParam);
//        hisConsumptionApi.addHisConsumption(hisConsumptionAddParam);

        //患者用药表
        AddByIdHisPrescriptionParam addByIdHisPrescriptionParam = new AddByIdHisPrescriptionParam();

        BeanUtils.copyProperties(addDoctorsOrderParam,addByIdHisPrescriptionParam);
        addByIdHisPrescriptionParam.setOrderFormId(hisOrderFormDtoResult.getData().getOrderFormId());
        List<AddHisPrescriptionParam> list1=new ArrayList<>();
        for (MedicationListParam listParam : addDoctorsOrderParam.getListParams()) {
            AddHisPrescriptionParam addHisPrescriptionParam = new AddHisPrescriptionParam();
            BeanUtils.copyProperties(listParam,addHisPrescriptionParam);
            addHisPrescriptionParam.setPrescriptionIspay(0);
            addHisPrescriptionParam.setPrescriptionIsreceive(0);
            list1.add(addHisPrescriptionParam);
        }
        addByIdHisPrescriptionParam.setList(list1);
        log.info("患者用药表请求类:{}",addByIdHisPrescriptionParam);
        hisPrescriptionQueryApi.newDrugs(addByIdHisPrescriptionParam);
        //查询患者身份证
        ListHisPatientParam listHisPatientParam = new ListHisPatientParam();
        listHisPatientParam.setPatientId(addDoctorsOrderParam.getPatientId());
        Result<PageDTO<HisPatientDto>> pageDTOResult = hisPatientQueryApi.queryPatientInformation(listHisPatientParam);
        //新增流水表
        CreateParam createParam = new CreateParam();
        createParam.setFlowingWaterType(2);
        createParam.setPatientCardId(pageDTOResult.getData().getList().get(0).getPatientCardid());
        createParam.setPatientId(addDoctorsOrderParam.getPatientId());
        createParam.setPatientName(addDoctorsOrderParam.getPatientName());
        createParam.setFlowingWaterHistory(0);
        createParam.setFlowingWaterMoney(new BigDecimal(totalPrice));
        payApi.createPay(createParam);
        return hisOrderFormDtoResult;
    }

    @Override
    public Result patientIdQueryOrder(PatientIdQueryOrderParam patientIdQueryOrderParam) {
        HisOrderFormParam hisOrderFormParam = new HisOrderFormParam();
        BeanUtils.copyProperties(patientIdQueryOrderParam,hisOrderFormParam);
        Result<com.woniu.dto.PageDTO<com.woniu.dto.HisOrderFormDto>> allHisOrderFormBy = hisOrderFormApi.getAllHisOrderFormBy(hisOrderFormParam);
        return allHisOrderFormBy;
    }

    @Override
    public Result modifyOrder(ModifyOrderParam modifyOrderParam) {
        HisOrderFormParam hisOrderFormParam = new HisOrderFormParam();
//        hisOrderFormParam.setOrderFormId(modifyOrderParam.getOrderFormId());
//
        hisOrderFormParam.setOrderFormId(modifyOrderParam.getOrderFormId());
        hisOrderFormParam.setPatientId(modifyOrderParam.getPatientId());

        Result<PageDTO<com.woniu.dto.HisOrderFormDto>> allHisOrderFormBy = hisOrderFormApi.getAllHisOrderFormBy(hisOrderFormParam);

        if(allHisOrderFormBy.getData().getList().get(0).getOrderFormState()==1){
            throw new DIYException(-6666,"该医嘱已执行");
        }
        ListHisPrescriptionParam listHisPrescriptionParam = new ListHisPrescriptionParam();
        listHisPrescriptionParam.setOrderFormId(modifyOrderParam.getOrderFormId());
        Result<PageDTO<HisPrescriptionDto>> pageDTOResult = hisPrescriptionQueryApi.queryMedication(listHisPrescriptionParam);
        for (HisPrescriptionDto hisPrescriptionDto : pageDTOResult.getData().getList()) {
            if(hisPrescriptionDto.getPrescriptionIspay()==1){
                throw new DIYException(-6666,"该用户已缴费");
            }
            if(hisPrescriptionDto.getPrescriptionIsreceive()==1){
                throw new DIYException(-6666,"该用户已领药");
            }
        }
        //修改医嘱表
        BeanUtils.copyProperties(modifyOrderParam,hisOrderFormParam);
        log.info("传入的值为:{}",hisOrderFormParam);
        Result<com.woniu.dto.HisOrderFormDto> hisOrderFormDtoResult = hisOrderFormApi.updateHisOrderForm(hisOrderFormParam);



        //修改住院消费表
//        HisConsumptionParam hisConsumptionParam = new HisConsumptionParam();
//        hisConsumptionApi.updateHisConsumption(hisConsumptionParam);
        //患者用药表
        DeleteMedicationParam deleteMedicationParam = new DeleteMedicationParam();
        deleteMedicationParam.setOrderFormId(modifyOrderParam.getOrderFormId());


        hisPrescriptionQueryApi.deleteByIdMedication(deleteMedicationParam);
        AddByIdHisPrescriptionParam addByIdHisPrescriptionParam = new AddByIdHisPrescriptionParam();
        BeanUtils.copyProperties(modifyOrderParam,addByIdHisPrescriptionParam);
        List<AddHisPrescriptionParam> list1=new ArrayList<>();
        for (MedicationListParam listParam : modifyOrderParam.getListParams()) {
            AddHisPrescriptionParam addHisPrescriptionParam = new AddHisPrescriptionParam();
            BeanUtils.copyProperties(listParam,addHisPrescriptionParam);
            addHisPrescriptionParam.setPrescriptionIspay(0);
            addHisPrescriptionParam.setPrescriptionIsreceive(0);
            list1.add(addHisPrescriptionParam);
        }
        addByIdHisPrescriptionParam.setList(list1);
        log.info("患者用药表请求类:{}",addByIdHisPrescriptionParam);
        hisPrescriptionQueryApi.newDrugs(addByIdHisPrescriptionParam);

        return hisOrderFormDtoResult;
    }

    @Override
    public Result newChecklist() {
        return null;
    }
}
